import re
import logging
import smtplib
from email.mime.text import MIMEText
from django.conf import settings

logger = logging.getLogger()
# 修改权限
logger.setLevel(logging.INFO)


def send_email(subject='', recver='', msg='', alert_msg=0, alert_dict=''):
    """
    发送邮件验证码
    :param subject: 邮件标题
    :param recver: 邮件接收方
    :param msg: 需要发送的验证码
    :param alert_msg: 是否需要发送告警信息: 1、是，0、否
    :param alert_dict: 告警信息字典：alert_dict = {
        'type': '监控类型', 'group': '监控分组', 'name': '监控名称', 'obj': '监控对象',
        'hostname':'监控主机', 'error': '错误信息', 'start_time': '故障时间', 'end_time': '恢复时间',
        'status': status, 'host': host,
        }
    :return:
    """

    if re.match(r".+@.+\.com$", recver):
        # 邮件服务器设置
        sender = settings.EMAIL_SENDER  # 发送方
        password = settings.EMAIL_PASSWD
        server = settings.EMAIL_SERVER
        port = settings.EMAIL_SERVER_PORT
        html_content = f"""
           <html>
           <body>
               <h2 style='color: #8a6d3b;display: inline-block'>验证码：</h2>
               <h2 style='color: #8a6d3b;display: inline-block'>{msg}</h2>
           </body>
           </html>
           """
        if alert_msg == 1:
            html_content = f"""
                <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <title>{alert_dict['type']}</title>
                </head>
                <body>
                <div>
                    <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">监控分组：</span>
                    <span style="color: #000000;font-weight: bold;">{alert_dict['group']}</span>
                </div>
                <div>
                    <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">监控类型：</span>
                    <span style="color: #000000;font-weight: bold;">{alert_dict['type']}</span>
                </div>
                <div>
                    <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">监控名称：</span>
                    <span style="color: #000000;font-weight: bold;">{alert_dict['name']}</span>
                </div>
                <div>
                    <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">监控对象：</span>
                    <span style="color: #000000;font-weight: bold;">{alert_dict['obj']}</span>
                </div>
                """
            if alert_dict['host'] == 1:
                html_content += f"""
                <div>
                    <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">监控主机：</span>
                    <span style="color: #000000;font-weight: bold;">{alert_dict['hostname']}</span>
                </div>
                """
            if alert_dict['status'] == 0:
                subject = "监控告警"
                html_content += f"""
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">告警状态：</span>
                        <span style="color: #ff0000;font-weight: bold;">告警中</span>
                    </div>
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">错误信息：</span>
                        <span style="color: #000000;font-weight: bold;">{alert_dict['error']}</span>
                    </div>
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">故障时间：</span>
                        <span style="color: #000000;font-weight: bold;">{alert_dict['start_time']}</span>
                    </div>
                    </body>
                    </html>
                   """
            else:
                subject = "异常恢复"
                html_content += f"""
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">告警状态：</span>
                        <span style="color: #2fff00;font-weight: bold;">已恢复</span>
                    </div>
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">错误信息：</span>
                        <span style="color: #000000;font-weight: bold;">{alert_dict['error']}</span>
                    </div>
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">故障时间：</span>
                        <span style="color: #000000;font-weight: bold;">{alert_dict['start_time']}</span>
                    </div>
                    <div>
                        <span style="color: #8a6d3b;font-weight: bold;font-size: 20px;">恢复时间：</span>
                        <span style="color: #000000;font-weight: bold;">{alert_dict['end_time']}</span>
                    </div>
                    </body>
                    </html>
                   """
        message = MIMEText(html_content, "html", "utf-8")
        # content 发送内容     "plain"文本格式   utf-8 编码格式

        message['Subject'] = subject  # 邮件标题
        message['To'] = recver  # 收件人
        message['From'] = sender  # 发件人
        try:
            smtp = smtplib.SMTP_SSL(server, port)  # 实例化smtp服务器
            smtp.login(sender, password)  # 发件人登录
            smtp.sendmail(sender, [recver], message.as_string())  # as_string 对 message 的消息进行了封装
            smtp.close()
        except Exception as e:
            logger.error(str(e))
            return 500
        return 200
    return 400
